// pages/love/love.js
const app = getApp()
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:{
      p:0,
      psize:10
    },
    recodes:[],
    current:0,
    ban:[
      { tit: "收到的表白", selected: true },
      { tit: "表白记录", selected: false }
    ],
    getlove:[],
    recode:[],
    lastX:"",
    lastY:"",
    hei: 0,
    scrollTop:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.getSystemInfo({
      success: res => {
        let windowHeight = (res.windowHeight * (750 / res.windowWidth));
        this.setData({
          hei: windowHeight - 70
        })
      }
    })
    wx.setNavigationBarTitle({
      title: '表白记录'
    })
    this.lovelist();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  up() {
    this.setData({
      'page.p': 0
    })
    if(this.data.current==0){
      this.lovelist(true);
    }else{
      this.init(true);
    }
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  down(){ //下一页
    let refresh=false;
    this.setData({
      'page.p':this.data.page.p + 1
    })
    if (this.data.current == 0) {
      this.lovelist(refresh);
    } else {
      this.init(refresh);
    }
  },
  agree(e){
    this.profess(0, e.currentTarget.dataset)
  },
  dis(e){
    this.profess(1, e.currentTarget.dataset)
  },
  profess(types,obj){
     let id = obj.id,
       index = obj.idx,
       status = obj.status;
    wx.request({
      url: base_url + '/Profess/changeStatus',
      method: 'POST',
      data: {
        'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
        'status': status,
        'profess_id':id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        if (res.data.success == 1) {
          let arr = this.data.getlove,txt;
          arr[index].left = 0
          if(types==0){
            txt ="已接收表白"
            arr[index].status = 1;
          }else{
            txt ="已拒绝表白"
            arr[index].status = 10;
          }
          this.setData({
            getlove: arr
          })
          wx.showToast({
            title:txt,
            icon: 'success',
            duration: 2000
          })
        } else {
          wx.showToast({
            title: '操作失败',
            icon: 'error',
            duration: 2000
          })
        }
      }
    })
  },
  handletouchmove(event) {
    let index = event.currentTarget.dataset.idx;
    let currentX = event.touches[0].pageX
    let currentY = event.touches[0].pageY
    let arr = this.data.getlove;
    if ((currentX - this.data.lastX) < 0){
      arr[index].left = 0;
    }
    else if (((currentX - this.data.lastX) > 0)){
      arr[index].left = -300;
    }
    this.setData({
      getlove:arr
    })
    // //将当前坐标进行保存以进行下一次计算
    this.data.lastX = currentX
    this.data.lastY = currentY
  },

  handletouchtart: function (event) {
   
    this.data.lastX = event.touches[0].pageX
    this.data.lastY = event.touches[0].pageY
  },
  went(e){
    let id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../lovedetail/love?id='+id,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  select(e){
    this.setData({
      scrollTop: 0
    })
    let index = e.target.dataset.idx,
        arr=this.data.ban;
    if(arr[index].selected) return false;
    for(let val of arr){
      val.selected=false;
    }
    arr[index].selected=true;
    this.setData({
      ban:arr,
      current:index,
      getlove: [],
      recodes: [],
      "page.p":0
    })
    if(index==0){
      this.lovelist();
    }else{
      this.init()
    }
  },
  lovelist(refresh = false){
    wx.showLoading({
      title: '加载中',
      success: () => {
        wx.request({
          url: base_url + '/Profess/getMyList',
          method: 'POST',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'p': this.data.page.p,
            'psize': this.data.page.psize
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            wx.hideLoading();
            if (res.data.success == 1 && res.data.data) {
              let recode = refresh ? res.data.data : this.data.getlove.concat(res.data.data);
              for(let val of recode){
                val.left=-300
              }
              this.setData({
                getlove: recode
              })
            } else {
              wx.showToast({
                title: '暂无数据',
                icon: 'none',
                duration: 2000
              })
            }
            //refresh && wx.stopPullDownRefresh();
          },
          fail: () => {
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  },
  init(refresh = false) {
    wx.showLoading({
      title: '加载中',
      success: () => {
        wx.request({
          url: base_url + '/Profess/getList',
          method: 'POST',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token,
            'p': this.data.page.p,
            'psize':this.data.page.psize
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            wx.hideLoading();
            if (res.data.success == 1 && res.data.data) {
              let recode = refresh ? res.data.data : this.data.recode.concat(res.data.data);
              this.setData({
                recodes:recode
              })
            } else {
              wx.showToast({
                title: '暂无数据',
                icon: 'none',
                duration: 2000
              })
            }
            //refresh && wx.stopPullDownRefresh();
          },
          fail:()=>{
            wx.showToast({
              title: res.data.msg,
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  },
})